package com.jiayang.hhotel.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.bean.CheckOutModel;
import com.jiayang.hhotel.util.StringUtil;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuAdapter;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/5/25 0025.
 * 结账退房子列表适配器
 */

public class CheckOutItemAdapter extends SwipeMenuAdapter<CheckOutItemAdapter.ViewHolder> {

    private Context t;
    private List<CheckOutModel.PTransDetailListDtosBean> mList;
    private HashMap<Integer,Boolean> isCheck;

    public CheckOutItemAdapter(Context context, List<CheckOutModel.PTransDetailListDtosBean> list){
        this.t=context;
        this.mList=list;
        this.isCheck =new HashMap<>();
        initData(false);
    }

    private void initData(boolean is) {
        for(int i = 0;i< mList.size();i++){
            isCheck.put(i,is);
            setIsCheck(isCheck);
        }
    }

    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        return LayoutInflater.from(t).inflate(R.layout.check_out_list_item,null);
    }

    @Override
    public ViewHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return new ViewHolder(realContentView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        CheckOutModel.PTransDetailListDtosBean model=mList.get(position);
        holder.roomNum.setText(model.getTrndescp());
        holder.money.setText(String.valueOf(model.getAmount()));
        if(getIsCheck().get(position)){
            holder.sel.setChecked(true);
        }else{
            holder.sel.setChecked(false);
        }
        holder.sel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(getIsCheck().get(position)){
                    isCheck.put(position,false);
                }else{
                    isCheck.put(position,true);
                    setIsCheck(isCheck);
                }
            }
        });
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        TextView roomNum;
        TextView money;
        CheckBox sel;

        public ViewHolder(View item){
            super(item);
            roomNum= (TextView) item.findViewById(R.id.tv_num);
            money = (TextView) item.findViewById(R.id.tv_money);
            sel= (CheckBox) item.findViewById(R.id.cb_sel);
        }
    }

    public HashMap<Integer,Boolean> getIsCheck(){
        return this.isCheck;
    }
    public void setIsCheck(HashMap<Integer,Boolean> isCheck){
        this.isCheck = isCheck;
    }

    //选择的账项代码拼接成字符串
    public String getMpSerial(){
        String str="";
        for(int i=0;i<getIsCheck().size();i++){
            if(getIsCheck().get(i)){
                str+=mList.get(i).getTrncode()+",";
            }
        }
        if(!StringUtil.isEmpty(str)){
            str=str.substring(0,str.length()-1);
        }
        return str;
    }

    //获取金额
    public double getAmount(){
        double money=0.00;
        for(int i=0;i<getIsCheck().size();i++){
            if(getIsCheck().get(i)){
                money+=mList.get(i).getAmount();
            }
        }
        return money;
    }

    /**
     * 设置是否全选
     * @param isAllSelCheck 是否
     */
    public void setIsAllSelCheck(boolean isAllSelCheck){
        initData(isAllSelCheck);
        notifyDataSetChanged();
    }
}
